/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-03-18     desktop       the first version
 */

#include <all_code.h>

/*  导入log工具 */
#define DBG_TAG "pwm_device.c"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

/* PWM设备句柄   */
struct rt_device_pwm *foot_pwm_dev_1; /* PWM设 备 句 柄 */
struct rt_device_pwm *foot_pwm_dev_2; /* PWM设 备 句 柄 */
struct rt_device_pwm *hand_pwm_dev_1; /* PWM设 备 句 柄 */

/*函数名：  pwm_foot_set
    作者：    Jingle_lan
    日期：    2022-03-17
    功能：   脚部电机调速，这是留给APP的调用接口   */
void pwm_foot_set(rt_uint32_t pwm1,rt_uint32_t pwm2)
{
    rt_pwm_set(foot_pwm_dev_1, PWM_DEV_CHANNEL_1,frequency,pwm1);
    rt_pwm_set(foot_pwm_dev_2, PWM_DEV_CHANNEL_2,frequency,pwm2);
}


/*函数名：  get_foot_pwm_pulse
    作者：    Jingle_lan
    日期：    2022-03-17
    功能：   脚部电机调速，传输参数直接是APP的速度参数，这里匹配好了对应的 脉冲宽度，*/
rt_uint32_t get_foot_pwm_pulse(rt_uint8_t speed)
{
   switch (speed)
   {

       /*  档位是0，%0占空比*/
        case 0:
        {
            return 0;
            break;
        }

        /*  档位是1，%5占空比*/
        case 1:
        {
            return 5000;//frequency * 0.05
            break;
        }

        /*  档位是2，%10占空比*/
        case 2:
        {
            return 10000;
            break;
        }

        /*  档位是3，%15占空比*/
        case 3:
        {
            return 15000;
            break;
        }

        /*  档位是4，%20占空比*/
        case 4:
        {
            return 20000;
            break;
        }

        /*  档位是5，%25占空比*/
        case 5:
        {
            return 25000;
            break;
        }

        /*  档位是6，%30占空比*/
        case 6:
        {
            return 30000;
            break;
        }

        /*  错误档位占空比为0，不启动*/
        default:
        {
            return 0;
            break;
        }
   }

}

/*函数名：  get_hand_pwm_pulse
    作者：    Jingle_lan
    日期：    2022-03-17
    功能：   手部舵机角度调节，直接传入角度控制参数，这里自动计算脉冲宽度，*/
void get_hand_pwm_pulse(rt_uint8_t angle)
{
    switch(angle)
    {

        case 0:
        {
            HAND_PWM_MAX = 1900;
            HAND_PWM_MIN = 1600;
            break;
        }

        case 1:
        {
            HAND_PWM_MAX = 2000;
            HAND_PWM_MIN = 1500;
            break;
        }

        case 2:
        {
            HAND_PWM_MAX = 2100;
            HAND_PWM_MIN = 1400;
            break;
        }

        case 3:
        {
            HAND_PWM_MAX = 2200;
            HAND_PWM_MIN = 1300;
            break;
        }

        case 4:
        {
            HAND_PWM_MAX = 2300;
            HAND_PWM_MIN = 1200;
            break;
        }

        case 5:
        {
            HAND_PWM_MAX = 2400;
            HAND_PWM_MIN = 1100;
            break;
        }

        case 6:
        {
            HAND_PWM_MAX = 2500;
            HAND_PWM_MIN = 1000;
            break;
        }
        default:
        {
            HAND_PWM_MAX = 0;
            HAND_PWM_MIN = 0;
            break;
        }
    }

}

/*函数名：  pwm_hand_set
    作者：    Jingle_lan
    日期：    2022-03-17
    功能：   手部舵机角度调节，直接传入角度控制参数，这里自动计算脉冲宽度，*/
void pwm_hand_set(rt_uint32_t pwm1)
{
    rt_pwm_set(hand_pwm_dev_1, PWM_DEV_CHANNEL_3,hand_frequency,pwm1);
}


/*函数名：  pwm_init
    作者：    Jingle_lan
    日期：    2022-03-16
    功能：  PWM外设初始化及控制线程创建*/
void pwm_device_init(void)
{
    foot_pwm_dev_1 = (struct rt_device_pwm*)rt_device_find(PWM_FOOT_NAME_1);
    foot_pwm_dev_2 = (struct rt_device_pwm*)rt_device_find(PWM_FOOT_NAME_2);
    hand_pwm_dev_1 = (struct rt_device_pwm*)rt_device_find(PWM_HAND_NAME_1);
    /*  初始化设置 频率为10K，占空比为0 */
    rt_pwm_set(foot_pwm_dev_1, PWM_DEV_CHANNEL_1,frequency,0);
    rt_pwm_set(foot_pwm_dev_2, PWM_DEV_CHANNEL_2,frequency,0);

    rt_pwm_set(hand_pwm_dev_1, PWM_DEV_CHANNEL_3,hand_frequency,0);
    /*  使能PWM，占空上面设置的是0 */
    //这里也可以选择不使能，如果不使能的话这两个IO口是高阻态，目前是高电平，不知道会不会对H桥产生影响，待定
    //最好是这里使能了PWM，去应用程序里 给PWM改变占空比就行，可以减少一些操作。
    rt_pwm_enable(foot_pwm_dev_1,PWM_DEV_CHANNEL_1);
    rt_pwm_enable(foot_pwm_dev_2,PWM_DEV_CHANNEL_2);

}
